package ATM_system;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    private Scanner peo = new Scanner(System.in);
    private ArrayList<Account> accounts = new ArrayList<>();


    //开始界面
    public void start() {
        while (true) {
            System.out.println("===欢迎您进入ATM系统===");
            System.out.println("1.用户登录");
            System.out.println("2.用户开户");
            System.out.println("请选择:");
            int command = peo.nextInt();
            switch (command) {
                case 1:
                    //用户登录
                    login();
                    break;
                case 2:
                    //用户开户
                    createAccount();
                    break;
                default:
                    System.out.println("没有该操作,请重新选择~~~");
            }
        }
    }

    //用户登录操作
    private void login() {
        System.out.println("===系统登录===");
        if (accounts.isEmpty()) {
            System.out.println("当前系统无任何账户,请先开户!!!");
            return;
        }
        while (true) {
            System.out.println("请您输入您的卡号:");
            String cardId = peo.next();
            Account acc = getAccountByCardId(cardId);
            if (acc == null) {
                System.out.println("您输入的卡号不存在~~");
            } else {
                while (true) {
                    System.out.println("请您输入您的密码:");
                    String password = peo.next();
                    if (acc.getPassword().equals(password)) {
                        System.out.println("恭喜您," + acc.getUserName() + "登录成功!" + "您的卡号为:" + acc.getId());
                        showUserCommand(acc);//进入登入界面
                        return;
                    } else {
                        System.out.println("您输入的密码错误,请重新输入:");
                    }
                }
            }
        }

    }

    //登录后的操作
    private void showUserCommand(Account loginAcc) {
        while (true) {
            System.out.println(loginAcc.getUserName() + "===您可以选择以下功能进行账户处理===");
            System.out.println("1.查询账户");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.密码修改");
            System.out.println("6.退出");
            System.out.println("7.注销当前账户");
            System.out.println("请选择");
            int command = peo.nextInt();
            switch (command) {
                case 1:
                    //查询账户
                    showLoginAccount(loginAcc);
                    break;
                case 2:
                    //存款
                    depositMoney(loginAcc);
                    break;
                case 3:
                    //取款
                    drawMoney(loginAcc);
                    break;
                case 4:
                    //转账
                    transferMoney(loginAcc);
                    break;
                case 5:
                    //密码修改
                    updatePassWord(loginAcc);
                    return;
                case 6:
                    //退出
                    System.out.println(loginAcc.getUserName() + "退出系统成功~~");
                    return;
                case 7:
                    //注销账号
                    if (deleteAccount(loginAcc)) {
                        return;
                    }
                    break;
                default:
                    System.out.println("您输入的操作不存在,请重新输入");
            }
        }
    }

    //账户密码修改
    private void updatePassWord(Account loginAcc) {
        //修改密码
        System.out.println("===账户密码修改操作===");
        while (true) {
            System.out.println("请您输入当前账户密码");
            String password = peo.next();
            //认证密码是否正确
            if (loginAcc.getPassword().equals(password)) {
                //认证通过
                while (true) {
                    System.out.println("请您输入新密码");
                    String newPassword = peo.next();
                    System.out.println("请您再次确认密码");
                    String newPassword2 = peo.next();
                    if (newPassword.equals(newPassword2)) {
                        //开始该密码
                        loginAcc.setPassword(newPassword);
                        System.out.println("修改密码成功~~");
                        return;
                    } else {
                        System.out.println("您输入的两次密码不一致,请重新输入~~");
                    }
                }
            } else {
                System.out.println("您当前输入的密码错误,请重新输入.");
            }
        }
    }

    //销户操作
    private boolean deleteAccount(Account loginAcc) {
        System.out.println("===进行销户操作===");
        //确定销户
        while (true) {
            System.out.println("是否确认销户?(y/n)");
            String command = peo.next();
            switch (command) {
                case "y":
                    //确认开始销户
                    if (loginAcc.getMoney() == 0) {
                        //开始销户
                        accounts.remove(loginAcc);
                        System.out.println("您的账户成功销户");
                        return true;
                    } else {
                        System.out.println("您的账户余额不为零,不允许销户");
                        return false;
                    }
                case "n":
                    //退出销户
                    System.out.println("取消销户操作成功~~");
                    return false;
                default:
                    System.out.println("输入有误,请重新输入");
            }
        }
    }

    //转账
    private void transferMoney(Account loginAcc) {
        System.out.println("===用户转帐===");
        //判断系统中账户是否只有一个
        if (accounts.size() < 2) {
            System.out.println("当前系统只有一个账户,无法转账");
            return;
        }
        //判断余额是否为0
        if (loginAcc.getMoney() == 0) {
            System.out.println("余额为0,无法转账");
            return;
        }

        //开始转账
        System.out.println("请您输入对方的卡号");
        String Id = peo.next();
        Account account = getAccountByCardId(Id);
        if (account == null) {
            System.out.println("您输入的对方的卡号不存在");
        } else {
            //认证姓氏
            String name = "*" + account.getUserName().substring(1);
            while (true) {
                System.out.println("请您输入 " + name + " 的姓氏");
                String preName = peo.next();
                if (account.getUserName().startsWith(preName)) {
                    //认证通过,真正转钱;
                    while (true) {
                        System.out.println("请您输入您要转账的金额:");
                        double money = peo.nextDouble();
                        //判断是否没有超过余额
                        if (loginAcc.getMoney() >= money) {
                            //转钱
                            loginAcc.setMoney(loginAcc.getMoney() - money);
                            account.setMoney(account.getMoney() + money);
                            System.out.println(loginAcc.getUserName() + "转账" + money + account.getUserName() + "成功");
                            return;
                        } else {
                            System.out.println("您余额不足,无法转账. 您的余额为:" + loginAcc.getMoney());
                        }
                    }
                } else {
                    System.out.println("您认证的姓氏有问题,请重新输入:");
                }
            }
        }
    }

    //取款操作
    private void drawMoney(Account loginAcc) {
        System.out.println("===取款操作===");
        //判断余额是否有一百元,没有就不让取
        if (loginAcc.getMoney() < 100) {
            System.out.println("您的账户不足一百元,不允许取款~~");
        } else {
            System.out.println("请您输入取款金额:");
            double money = peo.nextDouble();
            if (loginAcc.getMoney() >= money) {
                //判断是否超过取款限额
                if (money > loginAcc.getLimit()) {
                    System.out.println("您的取款金额超过了取款限额,您每次最多可取" + loginAcc.getLimit());
                } else {
                    loginAcc.setMoney(loginAcc.getMoney() - money);
                    System.out.println("您取款" + money + "成功,您的余额为:" + loginAcc.getMoney());
                }
            } else {
                System.out.println("余额不足, 您的账户余额为:" + loginAcc.getMoney());
            }
        }
    }

    //存款操作
    private void depositMoney(Account loginAcc) {
        System.out.println("===存款操作===");
        System.out.println("请您输入存款金额");
        double money = peo.nextDouble();
        //更新账户
        loginAcc.setMoney(money + loginAcc.getMoney());
        System.out.println("您存款:" + money + "成功! 余额为:" + loginAcc.getMoney());
    }

    //展示当前账户的信息
    private void showLoginAccount(Account loginAcc) {
        System.out.println("===当前您的账户信息如下===");
        System.out.println("卡号:" + loginAcc.getId());
        System.out.println("户主:" + loginAcc.getUserName());
        System.out.println("性别:" + loginAcc.getSex());
        System.out.println("余额:" + loginAcc.getMoney());
        System.out.println("每次取现额度:" + loginAcc.getLimit());
    }

    //用户的开户方法
    private void createAccount() {
        System.out.println("===系统开户操作===");
        //创建账户对象
        Account account = new Account();
        //用户输入开户信息

        //账户名称
        System.out.println("请输入您的账户名称");
        String name = peo.next();
        account.setUserName(name);
        //账户性别
        System.out.println("请输入您的账户性别");
        while (true) {
            char sex = peo.next().charAt(0);
            if (sex == '男' || sex == '女') {
                account.setSex(sex);
                break;
            } else {
                System.out.println("您输入的性别有误,请重新输入(男或女)");
            }
        }
        //账户密码
        while (true) {
            System.out.println("请输入您的账户密码");
            String password1 = peo.next();
            System.out.println("请再次确认账户密码");
            String password2 = peo.next();
            if (password1.equals(password2)) {
                account.setPassword(password1);
                break;
            } else {
                System.out.println("您输入的两次密码不一致,请重新输入~~");
            }
        }
        //账户的取现额度
        System.out.println("请输入您的账户取现额度");
        double limitMoney = peo.nextDouble();
        account.setLimit(limitMoney);

        //由系统自动生成卡号
        String newCardId = createCardId();
        account.setId(newCardId);

        //把账户对象放到集合中去
        accounts.add(account);
        System.out.println("恭喜你," + account.getUserName() + "开户成功!" + "您的卡号是:" + account.getId());
    }

    //生成随机卡号
    private String createCardId() {

        Random r = new Random();
        while (true) {
            String cardId = "";
            for (int i = 0; i < 8; i++) {
                int date = r.nextInt(10);
                cardId += date;
            }
            Account acc = getAccountByCardId(cardId);
            if (acc == null) {
                return cardId;
            }
        }
    }

    //根据卡号查找账户对象
    private Account getAccountByCardId(String cardId) {
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getId().equals(cardId)) {
                return acc;
            }
        }
        return null;
    }
}





